<?php

use Yansongda\Pay\Exceptions\BusinessException;

class store_hooks
{

    public $ci;

    /**
     * 构造函数
     */
    function __construct()
    {
        $this->ci = &get_instance();
    }


    // 返回申请的金额,
    function backMoney($param = []): bool
    {
        if (empty($param)) {
            return false;
        }
        return $this->ci->models('member')->operationMemberField($param['back_uid'], 'money', $param['money']);
    }

    // 增加已提现的金额, 并记录到支出的记录
    function withdrawal($param = [])
    {
        if (empty($param)) {
            return false;
        }
        $status = $this->ci->models('member')->operationMemberField($param['back_uid'], 'withdrawal', $param['money']);

        if ($status) {
            $this->ci->models('member/withdrawal')->addPayLog([
                'uid' => $param['back_uid'],
                'value' => -$param['money'],
                'module' => 'apply',
                'note' => '申请提现扣除' . $param['money'],
            ]);
        }
    }

    /**
     * 订单过期任务
     */
    function timeout()
    {
        $overDueSql = $this->ci->db->select('id,oid,cid,type')->where([
            'end_time <' => time(),
            'status' => 0
        ])->order_by('id asc')
            ->limit(10)->get('1_order_queue');
        while ($row = $overDueSql->unbuffered_row()) {
            if ($row->oid > 0) {
                if (in_array($row->type, ['box', 'favorite'])) {
                    $this->ci->models('favorite')->operateOrder($row->oid, ['order_status' => 9]);
                } elseif ($row->type == 'market') {
                    ### 更新市场表
                    $this->ci->models('market')->operateOrder($row->oid, ['order_status' => 9]);
                    $this->ci->models("market")->operateLock($row->cid, 0);
                }
                ### 更新队列状态
                $this->ci->models('favorite')->upQueue($row->id);
                ### 删除1小时内无效队列
                $this->ci->models('favorite')->delOverQueue(strtotime("-1 hours"));
            }
        }
    }

}
